<!DOCTYPE html>
<html>
<head>

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<title>Read Local Files</title>

<!--

$.get('file:///Users/afass/personal_src/adamfass/src/javascript/hello/local_files/')

open -a Google\ Chrome --args --allow-file-access-from-files

Not needed: --disable-web-security

no flags: cannot load
just allow-file...: works!

-->

<script
   type="text/javascript"
   src="require-jquery.js"></script>

</head>

<body>

<script>

/*
function onSuccess(data) {
  console.log('success!');
  window.data = data;
}

var x = $.get(
  'file:///Users/afass/personal_src/adamfass/src/javascript/hello/local_files/',
  onSuccess);
*/


function traverseDirectoryHelper(path, context) {
  console.log('traverseDirectoryHelper: ' + path);

  context.pendingRequestCount++;
  $.get(path, function(data) {
    var rowRegex = /addRow\("[^"]*","([^"]*)",([01]),/g;

    while (true) {
      var result = rowRegex.exec(data);
      if (result == null) {
        break;
      }
      if (result[1] == '.' || result[1] == '..') {
        continue;
      }

      var filename = result[1];
      var isDirectory = (result[2] == '1');

      if (isDirectory) {
        traverseDirectoryHelper(path + filename + '/', context);
      } else {
        context.processFile(path + filename);
      }
    }

    context.pendingRequestCount--;
    if (0 == context.pendingRequestCount) {
      context.onComplete();
    }
  });
};

function traverseDirectory(rootPath, processFile, onComplete) {
  var context = {
    pendingRequestCount: 0,
    processFile: processFile,
    onComplete: onComplete
  };
  traverseDirectoryHelper(rootPath, context);
};

/*
var rootPath =
  'file:///Users/afass/personal_src/adamfass/src/javascript/hello/local_files/';

var files = [];

function processFile(file) {
  files.push(file);
}

function onComplete() {
  console.log('complete!');
  for (var file in files) {
    console.log(files[file]);
  }
}

traverseDirectory(
  rootPath,
  processFile,
  onComplete);*/

function findFiles(path, includeFile, callback) {
  var files = [];

  function processFile(file) {
    if (includeFile(file)) {
      files.push(file);
    }
  }

  function onComplete() {
    callback(files);
  }

  traverseDirectory(path, processFile, onComplete);
}

findFiles(
  'file:///Users/afass/personal_src/adamfass/src/javascript/hello/local_files/',
  function(file) {
    return file.match(/file_._dir/);
  },
  function(files) {
    for (var file in files) {
      console.log(files[file]);
    }
  });

// var myRe = /ab*/g;
// var str = "abbcdefabh";
// var myArray;
// while ((myArray = myRe.exec(str)) !== null)
// {
//   var msg = "Found " + myArray[0] + ".  ";
//   msg += "Next match starts at " + myRe.lastIndex;
//   console.log(msg);
// }

/*
var iframe = document.createElement('iframe');
iframe.name = 'iframe_id';
iframe.width = 500;
iframe.height = 500;
iframe.src = '.';
document.body.appendChild(iframe);

var iframeWindow = document.getElementById('iframe_id').contentWindow;
*/

</script>


</body>
</html>
